$(function() {
    init();

    function init() {
        eventList();
        mui.init({
            pullRefresh: {
                container: ".view", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
                // 下拉
                down: {
                    auto: true, //可选,默认false.首次加载自动下拉刷新一次
                    callback: function() {
                            getQueryCart(function() {
                                mui('.view').pullRefresh().endPulldownToRefresh();
                            });
                        } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
                }
            }
        });
    }

    function eventList() {
        $("#lt_goods_list").on("change", ".goods_chk", function() {
            var chks = $(".goods_chk:checked");
            var total_prices = 0;
            for (var i = 0; i < chks.length; i++) {
                var element = chks[i];
                var tmpObj = $(element).closest("li").data("obj");
                var tmpPrice = tmpObj.price;
                var num = tmpObj.num;
                total_prices += (+tmpPrice * num);
            }
            total_prices = total_prices.toFixed(2);
            $(".total_price").text(total_prices);
        })
        $("#lt_goods_list").on("tap", ".item_delete", function() {
                var tmpObj = $(this).closest("li").data("obj");
                mui.confirm("你真的要删除吗", "温馨提示", ["确定", "取消"], function(etype) {
                    if (etype.value == 0) {
                        // 确定
                        var queryObj = {
                            id: [tmpObj.id]
                        }
                        deleteCart(queryObj, function() {
                            getQueryCart();
                        });
                    } else {
                        // 取消
                    }
                })
            })
            // 编辑
        $("#lt_goods_list").on("tap", ".item_edit", function() {
            // console.log(11);
            var obj = $(this).closest("li").data("obj");
            obj.productSize = $.formatSize(obj.productSize);
            var tplObj = {
                productSize: obj.productSize,
                size: obj.size,
                num: obj.num,
                productName: obj.productName
            };
            var html = template("editTemp", tplObj);
            // console.log(html);
            html = html.replace(/\n/g, "");
            // console.log(tplObj);
            mui.confirm(html, "编辑商品", ["确定", "取消"], function(etype) {
                if (etype.index == 0) {
                    var params = {
                        id: obj.id,
                        size: $(".size_btn.active").text(),
                        num: $(".num_val").val(),
                    };
                    $.post(location.origin + "/cart/updateCart", params, function(ret) {
                        if (ret.success) {
                            getQueryCart();
                        } else {
                            mui.toast(ret.message);
                        }
                    })
                } else if (etype.index == 1) {

                }
            })
            mui(".mui-numbox").numbox();
        });
        $("body").on("tap", ".size_btn", function() {
            $(this).addClass("active").siblings().removeClass("active");
        })
    }

    function getQueryCart(callback) {
        $.get(location.origin + "/cart/queryCart", function(ret) {
            if (ret.error && ret.error == 400) {
                sessionStorage.setItem("pageName", location.href);
                location.href = "./login.html";
            } else {
                var html = template("bbtemp", { arr: ret });
                $("#lt_goods_list").html(html);
                callback && callback();
            }
        })
    }

    function deleteCart(ids, callback) {

        $.get(location.origin + "/cart/deleteCart", ids, function(ret) {
            // console.log(ret);
            callback && callback();
        })

    }
})